package sv.gob.mh.dgii.exceptions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
@SuppressWarnings("unchecked")
public class SiitException extends Exception {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7388338617381685520L;

	private String key;
	private String context;
	private List jsiitErrors = new ArrayList();
	private String nextForward; //used by struts framework (view level)
	private boolean storelog; //flag for send the trace to the message queue
	private String bundle; //resource bundle used to get the message
	private String[] errorParams; //parameter for error message creation

	/***************
	 * CONSTRUCTORS*
	 * *************/
	
	public SiitException() {
		super();
	}	
	
	public SiitException(Throwable cause) {
		super(cause);
	}
	
	public SiitException(String message, Throwable cause) {
		super(message, cause);
	}	

	public SiitException(String context, String message, Throwable cause) {
		super(message, cause);
		this.context = context;
	}
	
	/*******************************
	 * BUSSINES ERRORS CONSTRUCTORS*
	 * *****************************/
	
	/**
	 * 
	 * @param key
	 * 			this is the key value to be searched by struts framework 
	 * 			in order to print the corresponding message error
	 */
	public SiitException(String key) {
		this.key = key;
		this.jsiitErrors = new ArrayList();
	}
	
	public SiitException(String key, String nextForward, String context) {
		this.key = key;
		this.jsiitErrors = new ArrayList();
		this.nextForward= nextForward;
		this.context = context;
	}
	
	public SiitException(String key, String context, String message, Throwable cause) {
		super(message, cause);
		this.key = key;
		this.context = context;
	}
	
	public SiitException(String context, String nextForward){
		this.context = context;
		this.nextForward = nextForward;
	}
		
	/*****************
	 * HELPER METHODS*
	 * ***************/
	
	
	public void addJsiitError(String key, Exception e){
		SiitException je = new SiitException(e);		
		je.setKey(key);
		jsiitErrors.add(je);
	}

	
	public void addJsiitError(String key, String context, Exception e){
		SiitException je = new SiitException(e);
		je.setKey(key);
		je.setContext(context);
		jsiitErrors.add(je);
	}
	
	
	public void addJsiitError(String key, String bundle, String[] params){
		SiitException je = new SiitException();
		je.setKey(key);
		je.setBundle(bundle);
		je.setErrorParams(params);
		jsiitErrors.add(je);
	}
	
	
	public void addJsiitError(String key, String context, String bundle, String[] params){
		SiitException je = new SiitException();
		je.setKey(key);
		je.setContext(context);
		je.setBundle(bundle);
		je.setErrorParams(params);
		jsiitErrors.add(je);
	}
	
	public void resetJsiitErrors(){
		jsiitErrors.clear();
	}
	
	
	/**
	 * For using when there's only business error and not an Exception
	 * of any kind
	 * @param key business error key message
	 */
	public void addBusinessError(String key){
		addJsiitError(key, null);
	}
	
	public boolean isBusinessError(){
		boolean errorFound = false;
		if(!jsiitErrors.isEmpty()){
			for (Iterator it =jsiitErrors.iterator();it.hasNext();){
				SiitException exception = (SiitException)jsiitErrors.iterator().next();
				if (exception.getKey()!=null){
					errorFound = true;
					break;
				}
			}
		}
		if (errorFound == false){
			if(getKey() != null && getKey().length() > 0){
				errorFound = true;
			}
		}
		return errorFound;
	}

	/**
	 * 
	 * @return true if does have a detail, false otherwise
	 */
	public boolean hasDetail(){
		return !this.jsiitErrors.isEmpty();
	}
	
	/**************************
	 * GETTER & SETTER METHODS*
	 **************************/

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	
	public List getJsiitErrors() {
		return jsiitErrors;
	}

	public void setJsiitErrors(List jsiitErrors) {
		this.jsiitErrors = jsiitErrors;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	
	public String getNextForward() {
		return nextForward;
	}

	public void setNextForward(String nextForward) {
		this.nextForward = nextForward;
	}
	
	
	public boolean isStorelog() {
		return storelog;
	}

	public void setStorelog(boolean storelog) {
		this.storelog = storelog;
	}	
	
	public String getBundle() {
		return bundle;
	}

	public void setBundle(String bundle) {
		this.bundle = bundle;
	}

	/**
	 * @return the errorParams
	 */
	public String[] getErrorParams() {
		return errorParams;
	}

	/**
	 * @param errorParams the errorParams to set
	 */
	public void setErrorParams(String[] errorParams) {
		this.errorParams = errorParams;
	}	
	
}
